{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.linear_model import LinearRegression\n",
    "from sklearn.datasets import load_boston"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [],
   "source": [
    "dataset = load_boston()\n",
    "\n",
    "X = dataset.data[:, 1:8]\n",
    "y = dataset.target\n",
    "\n",
    "# 划分训练集和测试集\n",
    "X_train = X[:-20]\n",
    "X_test = X[-20:]\n",
    "y_train = y[:-20]\n",
    "y_test = y[-20:]"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [],
   "source": [
    "regr = LinearRegression()\n",
    "regr.fit(X_train, y_train)\n",
    "y_test_pred = regr.predict(X_test)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [],
   "source": [
    "res_list=[]\n",
    "for i in range(20):\n",
    "    meta_data={'real': y_test[i], 'pred': y_test_pred[i],'acc':(y_test_pred[i]-y_test[i])/y_test[i]}\n",
    "    res_list.append(meta_data)\n",
    "\n",
    "pf=pd.DataFrame(res_list,columns=['real','pred','acc'])\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [],
   "source": [
    "pf.to_csv('./data1acc.csv',encoding='utf-8',index=False)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [],
   "source": [
    "import logging\n",
    "\n",
    "from boto3.session import Session\n",
    "from botocore.exceptions import ClientError\n",
    "class S3Handler():\n",
    "    def __init__(self):\n",
    "        access_key = \"29E2BC5B6851CD32568A\"\n",
    "        secret_key = \"WzVDQTFCMjlBMjU2NzQ4MTlFMTU3MjdDMzMyQTg1\"\n",
    "        url = \"http://scut.depts.bingosoft.net:29997\"  # 也可以是自己节点的地址\n",
    "        session = Session(access_key, secret_key)\n",
    "        self.s3_client = session.client('s3', endpoint_url=url)\n",
    "\n",
    "    def download_file(self, bucketName, objectName, fileName):\n",
    "        \"\"\"\n",
    "        下载文件\n",
    "        :param bucketName: 桶的名称\n",
    "        :param objectName: 文件的路径\n",
    "        :param fileName: 下载完成的文件的名称----注意：下载之后的文件默认储存在自己的python工程路径下\n",
    "        :return:\n",
    "        \"\"\"\n",
    "        self.s3_client.download_file(bucketName, objectName, fileName)\n",
    "\n",
    "    def upload_file(self, file_name, bucket, object_name=None):\n",
    "        \"\"\"\n",
    "        上传文件\n",
    "        :param file_name: 需要上传的文件的名称\n",
    "        :param bucket: S3中桶的名称\n",
    "        :param object_name: 需要上传到的路径，例如file/localfile/test\n",
    "        :return:\n",
    "        \"\"\"\n",
    "        if object_name is None:\n",
    "            object_name = file_name\n",
    "        try:\n",
    "            self.s3_client.upload_file(file_name, bucket, object_name, ExtraArgs={'ACL': 'public-read'})\n",
    "        except ClientError as e:\n",
    "            logging.error(e)\n",
    "            return False\n",
    "        return True\n",
    "\n",
    "    def list_object(self, bucketName):\n",
    "        \"\"\"\n",
    "        列出当前桶下所有的文件\n",
    "        :param bucketName:\n",
    "        :return:\n",
    "        \"\"\"\n",
    "        file_list = []\n",
    "        response = self.s3_client.list_objects_v2(\n",
    "            Bucket=bucketName,\n",
    "            # MaxKeys=1000  # 返回数量，如果为空则为全部\n",
    "        )\n",
    "        file_desc = response['Contents']\n",
    "        for f in file_desc:\n",
    "            print('file_name:{},file_size:{}'.format(f['Key'], f['Size']))\n",
    "            file_list.append(f['Key'])\n",
    "        return file_list"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [],
   "source": [
    "s3obj=S3Handler()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "True"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s3obj.upload_file('data1acc.csv','hujianqiao03')"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}